package com.power.project.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.power.base.service.impl.BaseServiceImpl;
import com.power.common.exception.CustomBizException;
import com.power.common.util.NanoidUtil;
import com.power.project.enums.MangeTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.power.project.model.ha_manage;
import com.power.project.dao.Ha_manageDao;
import com.power.project.service.Ha_manageService;

import org.springframework.transaction.annotation.Transactional;

/**
 *  管理员表业务层实现
 */
@Service
public class Ha_manageServiceImpl extends BaseServiceImpl implements Ha_manageService {

    @Autowired
    private Ha_manageDao dao;

    @Override
    public ha_manage getByUserid(String userid) {
        return dao.findByUserid(userid);
    }

    @Transactional
    @Override
    public void save(String userid, String tagerUserId) {
        ha_manage mangeInfo = dao.findByUserid(userid);
        if(!mangeInfo.getOwner().equals(MangeTypeEnum.SUPER_MANGE.getCode())){
            throw  new CustomBizException("此账号没有权限！请联系系统管理员");
        }
        ha_manage tagerMangeInfo = dao.findByUserid(tagerUserId);
        if(ObjUtil.isEmpty(tagerMangeInfo)){
            ha_manage mange = new ha_manage();
            mange.setId(NanoidUtil.randomNanoId());
            mange.setUserid(tagerUserId);
            mange.setOwner(MangeTypeEnum.REGULAR_MANGE.getCode());
            dao.save(mange);
        }
    }
    @Transactional
    @Override
    public void delete(String userid, String tagerUserId) {
        if(userid.equals(tagerUserId)){
            throw  new CustomBizException("不能删除属于自己的超级管理员权限！");
        }
        ha_manage mangeInfo = dao.findByUserid(userid);
        if(!mangeInfo.getOwner().equals(MangeTypeEnum.SUPER_MANGE.getCode())){
            throw  new CustomBizException("此账号没有权限！请联系系统管理员");
        }
        ha_manage tagerMangeInfo = dao.findByUserid(tagerUserId);
        if(ObjUtil.isNotEmpty(tagerMangeInfo)){
            dao.deleteByUserid(tagerUserId);
        }
    }
}
